package view;

import java.awt.BorderLayout;
import java.awt.Color;
import main.WinkelApplication;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Scrollbar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import model.Product;

public class ProductList extends JPanel implements MouseListener, ActionListener {

    private final int categoryId;
    private final int verticalPosition = 10;
    private final int offset = 40;
    
    public JPanel viewPanel;
    public JScrollPane sp;

    public ProductList(int categoryId) {
        super();
        this.setLayout(null);
        this.categoryId = categoryId;
        initComponents();
    }

    private void initComponents() {
        addTitle();
        addScrollPane();
        addProductItems();
        addBasket();
        addBackButton();
    }
    
    public void addScrollPane() {
        viewPanel = new JPanel(new BorderLayout());
        viewPanel.setBounds(0, 0, 521, 370);
        viewPanel.setVisible(true);
        
        sp = new JScrollPane(viewPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        sp.setBounds(20, 45, 521, 370);
        sp.setBorder(null);
        add(sp);
    }
    
    private void addBackButton(){
        JButton btnBack = new JButton("Terug");
        btnBack.setBounds(20,420,150,20);
        btnBack.setFont(WinkelApplication.FONT_12_BOLD); 
        btnBack.addActionListener(this);
        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/graphics/imgBack.png")));
        add(btnBack);
    }    

    private void addProductItems() {
        List<Product> products = WinkelApplication.getQueryManager().getProducts(categoryId);


        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);
            
            JLabel lblDot = new JLabel("\u2022");
            lblDot.setFont(WinkelApplication.FONT_12_BOLD);
            lblDot.setBounds(0, verticalPosition + i * offset, 10, 20);
            viewPanel.add(lblDot);

            JLabel lblProduct = new JLabel(product.getName());
            lblProduct.setBounds(15, verticalPosition + i * offset, 340, 20);
            lblProduct.setFont(WinkelApplication.FONT_12_BOLD);
            lblProduct.addMouseListener(this);
            lblProduct.setName(String.valueOf(product.getProductId()));
            lblProduct.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            viewPanel.add(lblProduct);

            JLabel lblDescription = new JLabel(product.getDescription());
            lblDescription.setBounds(15, verticalPosition + i * offset + 15, 340, 20);
            lblDescription.setFont(WinkelApplication.FONT_10_PLAIN);
            viewPanel.add(lblDescription);

            JLabel lblPrice = new JLabel(WinkelApplication.CURRENCY + product.getPriceFormatted());
            lblPrice.setBounds(360, verticalPosition  + i * offset + 15, 80, 20);
            lblPrice.setFont(WinkelApplication.FONT_12_BOLD);
            viewPanel.add(lblPrice);
            if (product.getStock()>0){
                JButton btnOrder = new JButton("Bestel");
                btnOrder.setBounds(415, verticalPosition  + i * offset + 15, 90, 20);
                btnOrder.setFont(WinkelApplication.FONT_12_BOLD);
                btnOrder.setName(String.valueOf(product.getProductId()));
                btnOrder.addActionListener(this);
                viewPanel.add(btnOrder);
            } else {
                JLabel btnOrder = new JLabel("Uitverkocht");
                btnOrder.setBounds(415, verticalPosition  + i * offset + 15, 90, 20);
                btnOrder.setFont(WinkelApplication.FONT_12_BOLD);
                btnOrder.setForeground(Color.gray);
                btnOrder.setName(String.valueOf(product.getProductId()));
                viewPanel.add(btnOrder);                
            }
        }
        JLabel lblEmpty = new JLabel("");
        viewPanel.add(lblEmpty);
    }

    private void addBasket() {
        BasketDisplay basket = new BasketDisplay();
        add(basket);
    }

    private void addTitle() {
        JLabel lblTitle1 = new JLabel();
        lblTitle1.setText("Winkelapplicatie");
        lblTitle1.setBounds(20, 20, 150, 20);
        lblTitle1.setFont(WinkelApplication.FONT_16_BOLD);
        lblTitle1.setName("-1");
        lblTitle1.addMouseListener(this);
        lblTitle1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        add(lblTitle1);

        JLabel lblTitle2 = new JLabel();
        lblTitle2.setText("-");
        lblTitle2.setBounds(170, 20, 20, 20);
        lblTitle2.setFont(WinkelApplication.FONT_16_BOLD);
        add(lblTitle2);
        JLabel lblTitle3 = new JLabel();
        lblTitle3.setText("Producten in " + WinkelApplication.getQueryManager().getCategoryName(categoryId));
        lblTitle3.setBounds(190, 20, 500, 20);
        lblTitle3.setFont(WinkelApplication.FONT_16_BOLD);
        add(lblTitle3);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        JLabel label = (JLabel) event.getSource();
        if (label.getName().equals("-1")) {
            // the user clicked on the title, go back to the first screen
            WinkelApplication.getInstance().showPanel(new view.CategoryList(),795,480);
        } else {
            WinkelApplication.getInstance().showPanel(new view.ProductDetails(Integer.parseInt(label.getName())),795,480);
        }
    }

    @Override
    public void mouseEntered(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mousePressed(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseExited(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String product_id = ((JButton) event.getSource()).getName();
        if (product_id==null){
            WinkelApplication.getInstance().showPanel(new CategoryList(),795,480);
        } else {
            Product product = WinkelApplication.getQueryManager().getProduct(Integer.parseInt(product_id));
            WinkelApplication.getBasket().addProduct(product);
        }
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawLine(20, 45, 540, 45);
    }
}
